home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / ucp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  7.6 KB  |  316 lines

  1. /* -------------------------------------------------------------------- */
  2. /* Variante del file ucp.c per sms_client 2.0.8r by JacK McKrak         */
  3. /* -------------------------------------------------------------------- */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8.  
  9. #include "common/common.h"
  10. #include "logfile/logfile.h"
  11. #include "driver.h"
  12. #include "error.h"
  13. #include "ascii.h"
  14. #include "comms/comms.h"
  15. #include "resource/resource.h"
  16.  
  17. /* -------------------------------------------------------------------- */
  18.  
  19. static struct ucp_env
  20. {
  21.     DRIVER_DEFAULT_ENV def;
  22.  
  23.     /* Place any extended driver    */
  24.     /* variables here         */
  25.  
  26. } driver_env;
  27.  
  28. /* -------------------------------------------------------------------- */
  29.  
  30. static     RESOURCE resource_list[] =
  31.     {
  32.         { RESOURCE_STRING,  "SMS_comms_params",
  33.      0, 1, NULL, 0,  "8N1",      0,       &(driver_env.def.comms_params)
  34.           },
  35.         { RESOURCE_STRING,  "SMS_centre_number",     
  36.     0, 1, NULL, 0,  NULL,       0,       &(driver_env.def.centre_number)
  37.           },
  38.         { RESOURCE_NUMERIC, "SMS_baud",         
  39.     0, 1, NULL, 0,  NULL,       9600, &(driver_env.def.baud)
  40.           },
  41.         { RESOURCE_NUMERIC, "SMS_deliver_timeout",     
  42.     0, 0, NULL, 0,  NULL,       30,   &(driver_env.def.deliver_timeout)
  43.           },
  44.         { RESOURCE_NUMERIC, "SMS_timeout",
  45.      0, 0, NULL, 0,  NULL,       10,   &(driver_env.def.timeout)
  46.           },
  47.         { RESOURCE_NUMERIC, "SMS_write_timeout",
  48.      0, 0, NULL, 0,  NULL,       10,   &(driver_env.def.write_timeout)
  49.           },
  50.         { RESOURCE_NUMERIC, "SMS_max_deliver",
  51.          0, 0, NULL, 0,  NULL,       1,    &(driver_env.def.max_deliver)
  52.           },
  53.         { RESOURCE_NULL,     NULL, 
  54.         0, 1, NULL, 0,  NULL,       0,    NULL
  55.           }
  56.     };
  57.  
  58. /* -------------------------------------------------------------------- */
  59.  
  60. #define DELIVERTIMEOUT         (driver_env.def.deliver_timeout)
  61. #define TIMEOUT         (driver_env.def.timeout)
  62. #define WRITETIMEOUT         (driver_env.def.write_timeout)
  63.  
  64. /* -------------------------------------------------------------------- */
  65.  
  66. #define FD            (driver_env.def.fd)
  67.  
  68. /* -------------------------------------------------------------------- */
  69.  
  70. static int UCP_sendmessage(char *msisdn, char *message);
  71. static int UCP_parse_response(char *string);
  72. static void UCP_hangup(void);
  73. static char *UCP_generate_checksum(const char *fmt, ...);
  74. static char *UCP_build_message(char *msisdn, char *message);
  75. static char *UCP_build_transaction(int transaction_id, char transaction_type,
  76.                                    int operation_type, const char *fmt, ...);
  77.  
  78. /* -------------------------------------------------------------------- */
  79. /* -------------------------------------------------------------------- */
  80. static char *UCP_generate_checksum(const char *fmt, ...)
  81. {
  82.     va_list args;
  83. static     char    buf[1024];
  84.     char    *ptr;
  85.     int    j;
  86.  
  87.  
  88.  
  89.     va_start(args, fmt);
  90. #if !defined(LINUX)
  91.     vsprintf(buf, fmt, args);
  92. #else
  93.     vsnprintf(buf, 1024, fmt, args);
  94. #endif
  95.     va_end(args);
  96.  
  97.  
  98.     /* ------------------------ */
  99.  
  100.     j = 0;
  101.     for (ptr = buf; *ptr != '\0'; ptr++)
  102.     {
  103.         j += *ptr;
  104.  
  105.         if (j >= 256)
  106.         {    j -= 256;
  107.         }
  108.     }
  109.  
  110.     sms_snprintf(buf, 1024, "%02X", j);
  111.     return buf;
  112. }
  113.  
  114.  
  115. /* -------------------------------------------------------------------- */
  116. /* -------------------------------------------------------------------- */
  117. static char *UCP_build_transaction(int transaction_id, char transaction_type,
  118.                                    int operation_type, const char *fmt, ...)
  119. {
  120.     va_list args;
  121. static    char    buf[1024];
  122.     char    header[1024],
  123.         data[1024],
  124.         codestring[1024];
  125.  
  126.     FILE *fcd;
  127.  
  128.  
  129.     va_start(args, fmt);
  130. #if !defined(LINUX)
  131.     vsprintf(data, fmt, args);
  132. #else
  133.     vsnprintf(data, 1024, fmt, args);
  134. #endif
  135.     va_end(args);
  136.  
  137.     /* ------------------------ */
  138.  
  139.  
  140.     fcd = fopen("codice","r");
  141.     fscanf(fcd,"%s",&codestring);
  142.     fclose(fcd);
  143.  
  144.     sms_snprintf(header, 1024, "%02d/%05d/%c", transaction_id,
  145.        sms_strlen(data) + sms_strlen(codestring) + 15, transaction_type);
  146.  
  147.     printf("<STX>%s/%s/%s/%s<ETX>\n",header,codestring,
  148.          data,UCP_generate_checksum("%s/%s/%s/",header,codestring,data));
  149.  
  150.     sms_snprintf(buf, 1024, "%c%s/%s/%s/%s%c", S_STX,
  151.                                  header,
  152.                                  codestring,
  153.                                  data,
  154.                                  UCP_generate_checksum("%s/%s/%s/", 
  155.                     header, codestring, data),
  156.                                  S_ETX);
  157.  
  158.     return buf;
  159. }
  160.  
  161.  
  162. /* -------------------------------------------------------------------- */
  163. /* -------------------------------------------------------------------- */
  164. static char *UCP_build_message(char *msisdn, char *message)
  165. {
  166.     char    ia5_message[1024],
  167.         *src,
  168.         *dest;
  169.  
  170.  
  171.     dest = ia5_message;
  172.     for (src = message; *src != '\0'; src++)
  173.     {
  174.         sms_snprintf(dest, 1024, "%02X", *src);
  175.         dest += 2;
  176.     }
  177.  
  178.     return UCP_build_transaction(1, 'O', 1, "%s", ia5_message);
  179. }
  180.  
  181.  
  182. /* -------------------------------------------------------------------- */
  183. /* Return Values:                            */
  184. /*     0 Positive ACK                         */
  185. /*     1 Negative ACK                            */
  186. /*    -1 Error                              */
  187. /* -------------------------------------------------------------------- */
  188. static int UCP_parse_response(char *string)
  189. {
  190.     int    result;
  191.  
  192.     int      transaction,
  193.          length,
  194.         type,
  195.         checksum;
  196.  
  197.     char     ack,
  198.         recipient[64],
  199.         timestamp[64];
  200.  
  201.     /* ------------------------------------------------------------ */
  202.     /* Example:                            */
  203.     /* <STX>01/00045/R/01/A/0041544180972:161298112313/A6<ETX>     */
  204.     /* <STX>01/00019/R/01/A//69<ETX>                */
  205.     /* ------------------------------------------------------------ */
  206.  
  207.     result = sscanf(string, "\002%02d/%05d/R/%02d/%c/%16[^:]:%12s/%02X\003",
  208.                     &transaction,
  209.                     &length,
  210.                     &type,
  211.                     &ack,
  212.                     recipient,
  213.                     timestamp,
  214.                     &checksum);
  215.  
  216.     if (result != 7)
  217.     {
  218.         result = sscanf(string, "\002%02d/%05d/R/%02d/%c//%02X\003",
  219.                         &transaction,
  220.                         &length,
  221.                         &type,
  222.                         &ack,
  223.                         &checksum);
  224.  
  225.         if (result != 5)
  226.         {    return -1;
  227.         }
  228.     }
  229.  
  230.     /* ---------------------------- */
  231.  
  232.     result = -1;
  233.  
  234.     if (ack == 'A')
  235.     {    result = 0;
  236.     }
  237.     else
  238.     if (ack == 'N')
  239.     {    result = 1;
  240.     }
  241.  
  242.     return result;
  243. }
  244.  
  245. /* -------------------------------------------------------------------- */
  246. /* -------------------------------------------------------------------- */
  247. static int UCP_sendmessage(char *msisdn, char *message)
  248. {
  249.     char     buf[MAX_RESPONSE_BUFSIZE],
  250.         *ucp_message;
  251.  
  252.     int    result;
  253.  
  254.  
  255.     ucp_message = UCP_build_message(msisdn, message);
  256.     twrite(FD, ucp_message, sms_strlen(ucp_message), WRITETIMEOUT);
  257.  
  258.     if (expstr(FD, buf, "\03", MAX_RESPONSE_BUFSIZE, DELIVERTIMEOUT) == 0)
  259.     {
  260.         lprintf(LOG_STANDARD, "SMSC Respsonse: %s\n", buf);
  261.  
  262.         result = UCP_parse_response(buf);
  263.         if (result == 0)
  264.         {
  265.             lprintf(LOG_STANDARD, "Received Acknowledgement\n");
  266.         }
  267.         else
  268.         if (result == 1)
  269.         {    lprintf(LOG_STANDARD, "Acknowledgement Failed\n");
  270.  
  271.             UCP_hangup();
  272.             return EUCP_ACKFAILED;
  273.         }
  274.         else
  275.         {    lprintf(LOG_STANDARD, "Bad Acknowledgement\n");
  276.  
  277.             UCP_hangup();
  278.             return EUCP_BADACK;
  279.         }
  280.     }
  281.     else
  282.     {    lprintf(LOG_STANDARD, "No Message Response\n");
  283.  
  284.         UCP_hangup();
  285.         return EUCP_NORESPONSE;
  286.     }
  287.  
  288.     return 0;
  289. }
  290.  
  291. /* -------------------------------------------------------------------- */
  292. /* -------------------------------------------------------------------- */
  293. static void UCP_hangup(void)
  294. {    default_hangup((DRIVER_DEFAULT_ENV *)(&driver_env));
  295. }
  296.  
  297. /* -------------------------------------------------------------------- */
  298. /* -------------------------------------------------------------------- */
  299. DEVICE_ENTRY ucp_device = {
  300.  
  301.     "UCP",
  302.     "1.0",
  303.     resource_list,
  304.     (DRIVER_DEFAULT_ENV *)(&driver_env),
  305.  
  306.     default_init,
  307.     default_main,
  308.     default_validate_numeric_id,
  309.     default_dial,
  310.     default_hangup,
  311.     default_send_disconnect,
  312.     default_single_deliver,
  313.     UCP_sendmessage,
  314.     default_login
  315. };
  316.